package com.qf.smartplatform.events.listeners;

import com.qf.smartplatform.pojo.Device;
import com.qf.smartplatform.pojo.DeviceOnOffLineEvent;
import com.qf.smartplatform.service.DeviceService;
import com.qf.smartplatform.utils.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * projectName:day12_27_Test
 * author:guoLei
 * time:2022/1/6 21:11
 * description:
 */
@Component
public class DeviceStatusChangeListener {
    private DeviceService deviceService;
    @Autowired
    public DeviceService getDeviceService() {
        return deviceService;
    }
    @EventListener
    @Async
    public void onEvent(DeviceOnOffLineEvent event){
        System.err.println("登录的ip" + event.getIp());
        Device device=new Device();
        device.setDeviceId(event.getId());//设备id
        device.setIsOnline(event.getStatus());//状态
        device.setConnectTime(event.getEventTime());//连接时间
        device.setLostConnectTime(event.getEventTime());
        if (StringUtils.hasText(event.getIp())){
            device.setConnectLocation(RequestUtil.getLocationByIp(event.getIp()));

        }
        deviceService.updateDeviceOnlineStaus(device);


    }
}
